package com.example.dataextraction.datasources;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

public class DynamicDataSource extends AbstractRoutingDataSource {
    private static final Logger log = LoggerFactory.getLogger(DynamicDataSource.class);
    private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();
    public static void set(String name){
        log.info("set datasource:{}", name);
        contextHolder.set(name);
    }
    public static String get(){
        return contextHolder.get();
    }
    public static void remove(){
     log.info("remove datasource:{}", contextHolder.get());        contextHolder.remove();
    }
    @Override
    protected Object determineCurrentLookupKey() {
        log.info("current datasource:{}", DynamicDataSource.get());
        return DynamicDataSource.get();
    }
}